package com.reeshkov.batstat;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.os.BatteryManager;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.text.format.DateFormat;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends Activity {

//    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //
        
        BroadcastReceiver batteryReceiver = new BroadcastReceiver() {
            int scale = -1;
            int level = -1;
            int voltage = -1;
            int temp = -1;
            TextView myTextViewVoltage = (TextView) findViewById(R.id.textView1);
            @SuppressLint("SimpleDateFormat") SimpleDateFormat sdf = new SimpleDateFormat("yy.MM.dd HH:mm:ss");
//            @Override
            public void onReceive(Context context, Intent intent) {
                level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
                scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
                temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1);
                voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1);
                Calendar c = Calendar.getInstance();
                String currentDateTimeString = sdf.format(c.getTime());
                String result = "\n"+currentDateTimeString +": "+"V: "+voltage+ " T: "+temp+ " Lvl: "+level+ " Sc: "+scale+ myTextViewVoltage.getText();
                //Log.e("BatteryManager", "level is "+level+"/"+scale+", temp is "+temp+", voltage is "+voltage);
                myTextViewVoltage.setText(result);
            }
        };
        IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        registerReceiver(batteryReceiver, filter);
    }
}
